var API_BASE_URL = "http://zzzpjx.cn:9082/api/v1";
$(()=>{
    $(document).on("click","a:not([onclick])",function(){
        if($(this.href.indexOf("javascript")>=0)) return true;
        location.href = this.href;
        return false;
    });


// 获取开奖记录数据
var responseData = {};
var timestamp = new Date().getTime();
$.get(API_BASE_URL + "/lottery/lotteries" + "?t=" + timestamp, function(resp, textStatus){
    // textStatus = success 成功
    console.log(textStatus, resp)
    if (resp.code == 200) {
        responseData.kjInfoArr = resp.data;
        handleKjInfo(responseData.kjInfoArr);
        return
    }
    console.log("消息处理错误")
})
})

/** lotteryArr 开奖记录对象数组
 *  "123"
 *  'abd' //
 *
 * */
/** lotteryArr 开奖记录对象数组 */
function handleKjInfo(lotteryArr) {
    // 画开奖结果
    /*
    [{"ballNum":1,"ballNumStr":"01","color":"red","fiveElement":"土","zodiac":"鼠","bigFlag":"小","singleDuble":"单","poultryType":"野兽"},{"ballNum":2,"ballNumStr":"02","color":"red","fiveElement":"木","zodiac":"猪","bigFlag":"小","singleDuble":"双","poultryType":"家禽"},{"ballNum":3,"ballNumStr":"03","color":"blue","fiveElement":"木","zodiac":"狗","bigFlag":"小","singleDuble":"单","poultryType":"家禽"},{"ballNum":4,"ballNumStr":"04","color":"blue","fiveElement":"火","zodiac":"鸡","bigFlag":"小","singleDuble":"双","poultryType":"家禽"},{"ballNum":5,"ballNumStr":"05","color":"green","fiveElement":"火","zodiac":"猴","bigFlag":"小","singleDuble":"单","poultryType":"野兽"},{"ballNum":6,"ballNumStr":"06","color":"green","fiveElement":"金","zodiac":"羊","bigFlag":"小","singleDuble":"双","poultryType":"家禽"},{"ballNum":7,"ballNumStr":"07","color":"red","fiveElement":"金","zodiac":"马","bigFlag":"小","singleDuble":"单","poultryType":"家禽"},{"ballNum":8,"ballNumStr":"08","color":"red","fiveElement":"水","zodiac":"蛇","bigFlag":"小","singleDuble":"双","poultryType":"野兽"},{"ballNum":9,"ballNumStr":"09","color":"blue","fiveElement":"水","zodiac":"龙","bigFlag":"小","singleDuble":"单","poultryType":"野兽"},{"ballNum":10,"ballNumStr":"10","color":"blue","fiveElement":"木","zodiac":"兔","bigFlag":"小","singleDuble":"双","poultryType":"野兽"},{"ballNum":11,"ballNumStr":"11","color":"green","fiveElement":"木","zodiac":"虎","bigFlag":"小","singleDuble":"单","poultryType":"野兽"},{"ballNum":12,"ballNumStr":"12","color":"red","fiveElement":"火","zodiac":"牛","bigFlag":"小","singleDuble":"双","poultryType":"家禽"},{"ballNum":13,"ballNumStr":"13","color":"red","fiveElement":"火","zodiac":"鼠","bigFlag":"小","singleDuble":"单","poultryType":"野兽"},{"ballNum":14,"ballNumStr":"14","color":"blue","fiveElement":"土","zodiac":"猪","bigFlag":"小","singleDuble":"双","poultryType":"家禽"},{"ballNum":15,"ballNumStr":"15","color":"blue","fiveElement":"土","zodiac":"狗","bigFlag":"小","singleDuble":"单","poultryType":"家禽"},{"ballNum":16,"ballNumStr":"16","color":"green","fiveElement":"水","zodiac":"鸡","bigFlag":"小","singleDuble":"双","poultryType":"家禽"},{"ballNum":17,"ballNumStr":"17","color":"green","fiveElement":"水","zodiac":"猴","bigFlag":"小","singleDuble":"单","poultryType":"野兽"},{"ballNum":18,"ballNumStr":"18","color":"red","fiveElement":"木","zodiac":"羊","bigFlag":"小","singleDuble":"双","poultryType":"家禽"},{"ballNum":19,"ballNumStr":"19","color":"red","fiveElement":"木","zodiac":"马","bigFlag":"小","singleDuble":"单","poultryType":"家禽"},{"ballNum":20,"ballNumStr":"20","color":"blue","fiveElement":"金","zodiac":"蛇","bigFlag":"小","singleDuble":"双","poultryType":"野兽"},{"ballNum":21,"ballNumStr":"21","color":"green","fiveElement":"金","zodiac":"龙","bigFlag":"小","singleDuble":"单","poultryType":"野兽"},{"ballNum":22,"ballNumStr":"22","color":"green","fiveElement":"土","zodiac":"兔","bigFlag":"小","singleDuble":"双","poultryType":"野兽"},{"ballNum":23,"ballNumStr":"23","color":"red","fiveElement":"土","zodiac":"虎","bigFlag":"小","singleDuble":"单","poultryType":"野兽"},{"ballNum":24,"ballNumStr":"24","color":"red","fiveElement":"水","zodiac":"牛","bigFlag":"小","singleDuble":"双","poultryType":"家禽"},{"ballNum":25,"ballNumStr":"25","color":"blue","fiveElement":"水","zodiac":"鼠","bigFlag":"大","singleDuble":"单","poultryType":"野兽"},{"ballNum":26,"ballNumStr":"26","color":"blue","fiveElement":"火","zodiac":"猪","bigFlag":"大","singleDuble":"双","poultryType":"家禽"},{"ballNum":27,"ballNumStr":"27","color":"green","fiveElement":"火","zodiac":"狗","bigFlag":"大","singleDuble":"单","poultryType":"家禽"},{"ballNum":28,"ballNumStr":"28","color":"green","fiveElement":"金","zodiac":"鸡","bigFlag":"大","singleDuble":"双","poultryType":"家禽"},{"ballNum":29,"ballNumStr":"29","color":"red","fiveElement":"金","zodiac":"猴","bigFlag":"大","singleDuble":"单","poultryType":"野兽"},{"ballNum":30,"ballNumStr":"30","color":"red","fiveElement":"土","zodiac":"羊","bigFlag":"大","singleDuble":"双","poultryType":"家禽"},{"ballNum":31,"ballNumStr":"31","color":"blue","fiveElement":"土","zodiac":"马","bigFlag":"大","singleDuble":"单","poultryType":"家禽"},{"ballNum":32,"ballNumStr":"32","color":"green","fiveElement":"木","zodiac":"蛇","bigFlag":"大","singleDuble":"双","poultryType":"野兽"},{"ballNum":33,"ballNumStr":"33","color":"green","fiveElement":"木","zodiac":"龙","bigFlag":"大","singleDuble":"单","poultryType":"野兽"},{"ballNum":34,"ballNumStr":"34","color":"red","fiveElement":"火","zodiac":"兔","bigFlag":"大","singleDuble":"双","poultryType":"野兽"},{"ballNum":35,"ballNumStr":"35","color":"red","fiveElement":"火","zodiac":"虎","bigFlag":"大","singleDuble":"单","poultryType":"野兽"},{"ballNum":36,"ballNumStr":"36","color":"blue","fiveElement":"金","zodiac":"牛","bigFlag":"大","singleDuble":"双","poultryType":"家禽"},{"ballNum":37,"ballNumStr":"37","color":"blue","fiveElement":"金","zodiac":"鼠","bigFlag":"大","singleDuble":"单","poultryType":"野兽"},{"ballNum":38,"ballNumStr":"38","color":"green","fiveElement":"水","zodiac":"猪","bigFlag":"大","singleDuble":"双","poultryType":"家禽"},{"ballNum":39,"ballNumStr":"39","color":"green","fiveElement":"水","zodiac":"狗","bigFlag":"大","singleDuble":"单","poultryType":"家禽"},{"ballNum":40,"ballNumStr":"40","color":"red","fiveElement":"木","zodiac":"鸡","bigFlag":"大","singleDuble":"双","poultryType":"家禽"},{"ballNum":41,"ballNumStr":"41","color":"blue","fiveElement":"木","zodiac":"猴","bigFlag":"大","singleDuble":"单","poultryType":"野兽"},{"ballNum":42,"ballNumStr":"42","color":"blue","fiveElement":"火","zodiac":"羊","bigFlag":"大","singleDuble":"双","poultryType":"家禽"},{"ballNum":43,"ballNumStr":"43","color":"green","fiveElement":"火","zodiac":"马","bigFlag":"大","singleDuble":"单","poultryType":"家禽"},{"ballNum":44,"ballNumStr":"44","color":"green","fiveElement":"土","zodiac":"蛇","bigFlag":"大","singleDuble":"双","poultryType":"野兽"},{"ballNum":45,"ballNumStr":"45","color":"red","fiveElement":"土","zodiac":"龙","bigFlag":"大","singleDuble":"单","poultryType":"野兽"},{"ballNum":46,"ballNumStr":"46","color":"red","fiveElement":"水","zodiac":"兔","bigFlag":"大","singleDuble":"双","poultryType":"野兽"},{"ballNum":47,"ballNumStr":"47","color":"blue","fiveElement":"水","zodiac":"虎","bigFlag":"大","singleDuble":"单","poultryType":"野兽"},{"ballNum":48,"ballNumStr":"48","color":"blue","fiveElement":"木","zodiac":"牛","bigFlag":"大","singleDuble":"双","poultryType":"家禽"},{"ballNum":49,"ballNumStr":"49","color":"green","fiveElement":"木","zodiac":"鼠","bigFlag":"大","singleDuble":"单","poultryType":"野兽"}]
    {"ballNum":1,"ballNumStr":"01","color":"red","fiveElement":"土","zodiac":"鼠","bigFlag":"小","singleDuble":"单","poultryType":"野兽"}
    */

    var resultArr = [];
    // 获取开奖号码信息
    var timestamp = new Date().getTime();
    var ballUrl = API_BASE_URL + "/ball/balls";
    $.get(ballUrl + "?t=" + timestamp, function(data, textStatus){
        // 显示对象一条记录的七个球信息， 对应数组中0-6
        // data 为球信息对象集合
        var ballInfoArr = data.data;

        // 遍历开奖信息,组装显示信息
        $.each(lotteryArr, function(index2, kjBall){
            // console.log(ele, kjBall)
            console.log(kjBall)
            var ballObj = {}; // 一次只会有一个球匹配到，开奖球不会重复

            // 总和单双
            var sum = kjBall.ball1 + kjBall.ball2 + kjBall.ball3 + kjBall.ball4 + kjBall.ball5 + kjBall.ball6 + kjBall.ball7;
            kjBall.singleDoubleAllBallSum = sum % 2 == 0 ? "双" : "单";
            kjBall.ball1Obj = getBallInfoByBallNum(kjBall.ball1, ballInfoArr);
            kjBall.ball2Obj = getBallInfoByBallNum(kjBall.ball2, ballInfoArr);
            kjBall.ball3Obj = getBallInfoByBallNum(kjBall.ball3, ballInfoArr);
            kjBall.ball4Obj = getBallInfoByBallNum(kjBall.ball4, ballInfoArr);
            kjBall.ball5Obj = getBallInfoByBallNum(kjBall.ball5, ballInfoArr);
            kjBall.ball6Obj = getBallInfoByBallNum(kjBall.ball6, ballInfoArr);
            kjBall.ball7Obj = getBallInfoByBallNum(kjBall.ball7, ballInfoArr);
        })
        console.log(lotteryArr)

        var showData = {lotteryArr: lotteryArr}
        var html = template('recordContent', showData);
        document.getElementById('recordContentDiv').innerHTML = html;
    });
}
// 根据开奖球号码获取球信息
function getBallInfoByBallNum(kjBallNum, ballInfoArr) {
    var ballObj = null;
    $.each(ballInfoArr, function(index, ele){
        // console.log(ele)
        if (kjBallNum == ele.ballNum) {
            ballObj = ele;

            // 构造显示信息
            if (ballObj.color == "red") {
                ballObj.colorStr = '红'
            }
            if (ballObj.color == "green") {
                ballObj.colorStr = '绿'
            }
            if (ballObj.color == "blue") {
                ballObj.colorStr = '蓝'
            }

            // 单数字分隔后合单双

            var sum = kjBallNum;
            if(kjBallNum > 9) {
                sum = Math.floor(kjBallNum / 10) + parseInt((kjBallNum + "").substr(1,1));
            }
            ballObj.singleDoubleSum = sum % 2 == 0 ? "双" : "单"

            return false;
        }
    });
    return ballObj;
}